import java.util.HashSet;
import java.util.Set;
import java.util.Scanner;

public class process 
{
	int InputNodeId;
	int OutputNodeId;
	double InputNodeValue;
	double WeightValue;
	Set OutSet = new HashSet();  
	Set InSet = new HashSet();  
	int high;
	
	public void ProcessNeuron()
	{
		Main m = new Main();

		for(int i=0; i<m.wnum ;i++) //find all outputs
		{
			OutputNodeId = m.w[i].OutNode;
			OutSet.add(OutputNodeId); //make a set of outputs
		}
		
		System.out.println("here is a set of outputs: "+OutSet.toString());

		Object OutArr[] = OutSet.toArray();
		
		
		for(int i=0; i<OutArr.length; i++) //find corresponding inputs to those outputs
		{
			double sum=0;
			int output = OutArr[i].hashCode();
			
			for(int j=0; j<Main.wnum; j++) //find this output through the links
			{
				if(Main.w[j].OutNode==output)
				{
					//System.out.println("The link of output "+output+" has an input of "+Main.w[j].InNode);
					double weight = Main.w[j].weightValue;
					//System.out.println("link's weight is "+weight);
					double invalue = Main.n[Main.w[j].InNode].value;
					//System.out.println("input of "+Main.w[j].InNode+" has value "+invalue);
					
					double calculation = weight*invalue;
					sum=sum+calculation;
					//System.out.println();
					
				}
			}
			System.out.println(OutArr[i]+" now has an input of "+sum);
			
			Main.n[OutArr[i].hashCode()].value = sum;
		}
		
		for(int q=0; q<Main.wnum; q++)
			System.out.println("weight "+q+": "+Main.w[q].weightValue);
	}
	
	
	public void Learn()
	{
		Object OutArr[] = OutSet.toArray();
		high=0;
		boolean found = true;
		for(int i=0; i<OutArr.length; i++) //find biggest output value
		{
			if(found==false)
			{
				if(Main.n[OutArr[i].hashCode()].value>Main.n[high].value)
				{
					found=true;
				}
			}
			if(found==true)
			{
				high = OutArr[i].hashCode();
				found=false;
			}
		}
		System.out.println("winner is neuron "+high);
		
		Ask();
	}
	
	public void Ask()
	{
		Scanner kb = new Scanner(System.in);
		System.out.println("Is "+high+" the correct winner? (y/n)");
		String answer = kb.next();
		
		for(int i=0; i<Main.wnum ;i++) //find all inputs
		{
			InputNodeId = Main.w[i].InNode;
			InSet.add(InputNodeId); //make a set of inputs
		}
		
		if(answer.equalsIgnoreCase("y")) //strengthen the correct weight, weaken the incorrect ones
		{ 
			for(int i=0; i<Main.wnum; i++) //look through the weights
			{
				if(Main.w[i].InNode>0)
				{
					
					Main.w[i].weightValue = Main.w[i].weightValue + Main.w[i].weightValue*((double)1/InSet.size());
					
				}
				else
				{
					Main.w[i].weightValue = Main.w[i].weightValue - Main.w[i].weightValue*((double)1/InSet.size());
				}
			}
		}
		else if(answer.equalsIgnoreCase("n"))
		{
			for(int i=0; i<Main.wnum; i++) //look through the weights
			{
				if(Main.w[i].InNode>0)
				{
					Main.w[i].weightValue = Main.w[i].weightValue - Main.w[i].weightValue*((double)1/InSet.size());
				}
				else
				{
					Main.w[i].weightValue = Main.w[i].weightValue + Main.w[i].weightValue*((double)1/InSet.size());
				}
			}
		}
		else
		{
			Ask();
		}
		
		for(int q=0; q<Main.wnum; q++)
			System.out.println("weight "+q+": "+Main.w[q].weightValue);
		
	}
	
}
